package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.flyco.roundview.RoundTextView;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.CustomGridItemDecoration;
import com.kmproject.haifu.app.utils.ExtensionUtilKt;
import com.kmproject.haifu.app.utils.ViewExtensionKt;
import com.kmproject.haifu.di.component.DaggerConvenientPaymentRecordComponent;
import com.kmproject.haifu.mvp.contract.ConvenientPaymentRecordContract;
import com.kmproject.haifu.mvp.model.entity.beans.ConvenientPaymentRecordBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsGetConvenientPaymentRecord;
import com.kmproject.haifu.mvp.presenter.ConvenientPaymentRecordPresenter;
import com.kmproject.haifu.mvp.ui.adapter.BaseRecyclerAdapter;
import com.kmproject.haifu.mvp.ui.holder.SmartViewHolder;
import com.kmproject.haifu.mvp.ui.widget.pullRefresh.OnRefreshLoadMoreListener2;
import com.kmproject.haifu.mvp.ui.widget.pullRefresh.PullToRefreshView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.widget.picker.bis.DataPickerDialog;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/30/2019 17:42
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class ConvenientPaymentRecordActivity extends BaseActivity<ConvenientPaymentRecordPresenter> implements ConvenientPaymentRecordContract.View {
    @BindView(R.id.rtv_type)
    RoundTextView rtv_type;
    @BindView(R.id.srl_record_list)
    PullToRefreshView srl_record_list;
    @BindView(R.id.rv_record_list)
    RecyclerView rv_record_list;
    @BindView(R.id.i_empty)
    View empty;

    private List<String> typeList;
    private ReqParamsGetConvenientPaymentRecord reqParamsGetConvenientPaymentRecord;
    private BaseRecyclerAdapter<ConvenientPaymentRecordBean> adapter;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerConvenientPaymentRecordComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_convenient_payment_record; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        typeList = Arrays.asList("全部类型", "话费充值", "缴纳电费", "缴纳水费", "缴纳网费");
        reqParamsGetConvenientPaymentRecord = new ReqParamsGetConvenientPaymentRecord();

        rv_record_list.addItemDecoration(new CustomGridItemDecoration.Builder(this).setVerticalSpan(0f).
                setHorizontalSpan(1f).setColor(ContextCompat.getColor(this, R.color.common_menu_text)).setShowLastLine(false).build());
        ArmsUtils.configRecyclerView(rv_record_list, new LinearLayoutManager(this));
        rv_record_list.setItemAnimator(new DefaultItemAnimator());
        srl_record_list.setEnableRefresh(true);
        srl_record_list.setEnableAutoLoadMore(false);
        srl_record_list.setEnableHeaderTranslationContent(true);

        adapter = new BaseRecyclerAdapter<ConvenientPaymentRecordBean>(R.layout.item_convenient_payment_record) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, ConvenientPaymentRecordBean model, int position) {
                switch (model.getInvestType()) {//充值类型：mobile话费，electricity电费，water水费,network网费
                    case "mobile":
                        holder.image(R.id.imv_type_icon, R.mipmap.icon_payment_record_mobile);
                        holder.text(R.id.tv_type_name, "话费充值");
                        break;
                    case "electricity":
                        holder.image(R.id.imv_type_icon, R.mipmap.icon_payment_record_electricity);
                        holder.text(R.id.tv_type_name, "缴纳电费");
                        break;
                    case "water":
                        holder.image(R.id.imv_type_icon, R.mipmap.icon_payment_record_water);
                        holder.text(R.id.tv_type_name, "缴纳水费");
                        break;
                    case "network":
                        holder.image(R.id.imv_type_icon, R.mipmap.icon_payment_record_network);
                        holder.text(R.id.tv_type_name, "缴纳网费");
                        break;
                }
                holder.text(R.id.tv_amount, model.getTotalMoney() + "元");
                holder.text(R.id.tv_time, model.getCreateTime());
            }
        };
        rv_record_list.setAdapter(adapter);

        adapter.setOnItemClickListener((parent, view, position, id) -> {
            ArmsUtils.makeText(ConvenientPaymentRecordActivity.this, "index:" + adapter.getRealItemPosition(position));
            Intent intent = new Intent(ConvenientPaymentRecordActivity.this, ConvenientPaymentRecordDetailActivity.class);
            intent.putExtra("convenientPaymentRecordBean", adapter.get(position));
            ArmsUtils.startActivity(intent);
        });

        srl_record_list.setOnRefreshLoadMoreListener2(new OnRefreshLoadMoreListener2() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout, int curPageNo) {
                // 加载更多
                reqParamsGetConvenientPaymentRecord.setPage(curPageNo);
                getListData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout, int curPageNo) {
                // 刷新
                reqParamsGetConvenientPaymentRecord.setPage(curPageNo);
                getListData();
            }
        });
        getListData();
    }

    private void getListData() {
        assert mPresenter != null;
        mPresenter.convenientPaymentRecord(reqParamsGetConvenientPaymentRecord);
    }

    @OnClick({R.id.rtv_type})
    public void onClick(View view) {
        if (view.getId() == R.id.rtv_type) {
            ExtensionUtilKt.showDataPw(this, "选择充值类型", typeList, 0, new DataPickerDialog.OnDataSelectedListener() {
                @Override
                public void onDataSelected(String itemValue, int position) {
                    rtv_type.setText(itemValue);
                    switch (position) {//充值类型：mobile-话费，electricity-电费，water-水费,network-网费,不传查询所有
                        case 0:
                            reqParamsGetConvenientPaymentRecord.setInvestType(null);
                            break;
                        case 1:
                            reqParamsGetConvenientPaymentRecord.setInvestType("mobile");
                            break;
                        case 2:
                            reqParamsGetConvenientPaymentRecord.setInvestType("electricity");
                            break;
                        case 3:
                            reqParamsGetConvenientPaymentRecord.setInvestType("water");
                            break;
                        case 4:
                            reqParamsGetConvenientPaymentRecord.setInvestType("network");
                            break;
                    }
                }

                @Override
                public void onCancel() {
                    ArmsUtils.makeText(ConvenientPaymentRecordActivity.this, getString(R.string.cancel));
                }
            });
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void convenientPaymentRecordSuccess(List<ConvenientPaymentRecordBean> list) {
        srl_record_list.finishLoadMore();
        srl_record_list.finishRefresh();
        if (list != null && !list.isEmpty()) {
            ViewExtensionKt.visible(empty, false);
            ViewExtensionKt.visible(rv_record_list, true);
            if (ViewExtensionKt.isPullDown(srl_record_list, list.size(), reqParamsGetConvenientPaymentRecord.getLimit())) {
                adapter.refresh(list);
            } else {
                adapter.loadMore(list);
            }
        } else {
            if (reqParamsGetConvenientPaymentRecord.getPage() > 1) {
                ViewExtensionKt.visible(empty, false);
                ViewExtensionKt.visible(rv_record_list, true);
            } else {
                ViewExtensionKt.visible(empty, true);
                ViewExtensionKt.visible(rv_record_list, false);
            }
        }
    }

    @Override
    public void getListFailed() {
        srl_record_list.finishLoadMore();
        srl_record_list.finishRefresh();
        if (reqParamsGetConvenientPaymentRecord.getPage() > 1) {
            reqParamsGetConvenientPaymentRecord.setPage(reqParamsGetConvenientPaymentRecord.getPage() - 1);
        }
    }
}
